<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Examtopic;
use Illuminate\Http\Request;

class ExamController extends Controller {

	public function __construct() {
		$this->middleware('role:exam');
	}

	/**
	 * 进入习题列表页面
	 */
	public function getTopic() {
		return view('admin/exam/examtopic_list');
	}

	/**
	 * 获取分页数据
	 */
	public function getPageRows(Request $request) {
		$page = to_limit_lng($request->input('page'), 1);
		$limit = to_limit_lng($request->input('limit'), 1);
		$offset = ($page - 1) * $limit;

		$query = Examtopic::where('f_id', 0);
		$total_rows = $query->count();
		$rows = $query->orderBy('id', 'ASC')->skip($offset)->take($limit)->get();
		foreach ($rows as $row) {
			$row->typeText = $this->getTypeArr()[$row->type];
		}
		$data['rows'] = $rows;

		$data['total_rows'] = $total_rows;
		$data['page_count'] = ceil($total_rows / $limit);
		$data['page'] = $page;

		return $this->response($data);
	}

	/**
	 * 获取习题类型
	 */
	public function getTypeArr() {
		return [
			1 => '单项选择',
			2 => '填空题',
			3 => '完形填空',
			4 => '阅读理解',
		];
	}

	/**
	 * 进入增加习题页面
	 */
	public function getAddTopic() {
		$data['typeArr'] = $this->getTypeArr();
		return view('admin/exam/examtopic_add', $data);
	}

	/**
	 * 增加习题
	 */
	public function postInsert(Request $request) {
		$type = intval($request->input('type'));
		switch ($type) {
		case 1:
			return $this->addChoiseTopic($request);
		case 2:
			return $this->addFillblankTopic($request);
		case 3:
			return $this->addClozetestTopic($request);
		case 4:
			return $this->addReadingTopic($request);
		default:
			return $this->error('无效的题型');
		}
	}

	/**
	 * 单项选择题添加
	 */
	private function addChoiseTopic($request) {
		$type = intval($request->input('type'));
		$topic = strval($request->input('topic'));
		if (empty($topic)) {
			return $this->error('无效的题目');
		}

		$opts = $request->input('options');
		if (!is_array($opts)) {
			return $this->error('参数填写不完整');
		}
		$options = [];
		foreach ($opts as $k => $opt) {
			$options[chr($k + 65)] = $opt;
		}
		$options = json_encode($options);
		$answer = strval($request->input('answer'));

		$data = compact('type', 'topic', 'options', 'answer');
		$row = new Examtopic;
		foreach ($data as $key => $val) {
			$row->$key = $val;
		}
		$row->save();
		return $this->response($row);
	}

	/**
	 * 填空题添加
	 */
	private function addFillblankTopic($request) {
		$type = intval($request->input('type'));
		$topic = strval($request->input('topic'));
		if (empty($topic)) {
			return $this->error('无效的题目');
		}

		$answer = $request->input('answer');
		if (!is_array($answer)) {
			return $this->error('参数填写不完整');
		}
		$answer = json_encode($answer);

		$options = '';
		$data = compact('type', 'topic', 'options', 'answer');
		$row = new Examtopic;
		foreach ($data as $key => $val) {
			$row->$key = $val;
		}
		$row->save();
		return $this->response($row);
	}

	/**
	 * 添加完型填空题
	 */
	private function addClozetestTopic($request) {
		$type = intval($request->input('type'));
		$topic = strval($request->input('topic'));
		if (empty($topic)) {
			return $this->error('无效的题目');
		}
		$trow = new Examtopic;
		$trow->type = $type;
		$trow->topic = $topic;
		$trow->options = '';
		$trow->answer = '';
		$trow->save();

		$opts = $request->input('options');
		$answers = $request->input('answer');
		if (!is_array($opts) OR !is_array($answers)) {
			return $this->error('参数填写不完整');
		}

		for ($i = 0; $i < count($opts); $i++) {
			if (array_key_exists($i, $opts)) {
				$arr = array_slice($opts, $i, 4);
				if (count($arr) != 4) {
					$this->error('参数格式错误');
				}
				$options = [];
				foreach ($arr as $k => $v) {
					$options[chr($k + 65)] = $v;
				}
				$options = json_encode($options);
				$answer = array_shift($answers);
				$f_id = $trow->id;
				$topic = $i / 4 + 1;
				$data = compact('f_id', 'topic', 'options', 'answer');
				$row = new Examtopic;
				foreach ($data as $key => $val) {
					$row->$key = $val;
				}
				$row->save();
				$i = $i + 3;
			}
		}
		return $this->response($trow);
	}

	/**
	 * 增加阅读理解题
	 */
	private function addReadingTopic($request) {
		$type = intval($request->input('type'));
		$topic = strval($request->input('topic'));
		if (empty($topic)) {
			return $this->error('无效的题目');
		}
		$trow = new Examtopic;
		$trow->type = $type;
		$trow->topic = $topic;
		$trow->options = '';
		$trow->answer = '';
		$trow->save();

		$subtopics = $request->input('subtopic');
		$opts = $request->input('options');
		$answers = $request->input('answer');
		if (!is_array($subtopics) OR !is_array($opts) OR !is_array($answers)) {
			return $this->error('参数填写不完整');
		}

		foreach ($subtopics as $i => $topic) {
			$arr = array_slice($opts, $i * 4, 4);
			$options = [];
			foreach ($arr as $k => $v) {
				$options[chr($k + 65)] = $v;
			}
			$options = json_encode($options);
			$answer = $answers[$i];
			$f_id = $trow->id;
			$data = compact('f_id', 'topic', 'options', 'answer');
			$row = new Examtopic;
			foreach ($data as $key => $val) {
				$row->$key = $val;
			}
			$row->save();
		}
		return $this->response($trow);
	}

	/**
	 * 进入习题编辑页面
	 */
	public function getEdit(Request $request) {
		$id = intval($request->input('id'));
		$row = Examtopic::find($id);
		if (empty($row)) {
			return $this->warning('无效的题目');
		}
		switch ($row->type) {
		case 1:
			return $this->editChoiseTopic($row);
		case 2:
			return $this->editFillblankTopic($row);
		case 3:
			return $this->editClozetestTopic($row);
		case 4:
			return $this->editReadingTopic($row);
		default:
			return $this->warning('无效的题型');
		}
	}

	/**
	 * 进入单项选择题编辑页面
	 */
	private function editChoiseTopic(Examtopic $topic) {
		$topic->options = json_decode($topic->options, true);
		$data = $topic->toArray();
		return view('admin/exam/choisetopic_edit', $data);
	}

	/**
	 * 进入填空题编辑页面
	 */
	private function editFillblankTopic(Examtopic $topic) {
		$topic->answers = json_decode($topic->answer, true);
		$data = $topic->toArray();
		return view('admin/exam/fillblanktopic_edit', $topic);
	}

	/**
	 * 进入完型填空题编辑页面
	 */
	private function editClozetestTopic(Examtopic $topic) {
		$data['id'] = $topic->id;
		$data['topic'] = $topic->topic;
		$rows = Examtopic::where('f_id', $topic->id)->orderBy('id', 'ASC')->get();
		foreach ($rows as $row) {
			$row->options = json_decode($row->options, true);
		}
		$data['rows'] = $rows;
		return view('admin/exam/clozetesttopic_edit', $data);
	}

	/**
	 * 进入阅读理解题编辑页面
	 */
	private function editReadingTopic(Examtopic $topic) {
		$data['id'] = $topic->id;
		$data['topic'] = $topic->topic;
		$rows = Examtopic::where('f_id', $topic->id)->orderBy('id', 'ASC')->get();
		foreach ($rows as $row) {
			$row->options = json_decode($row->options, true);
		}
		$data['rows'] = $rows;
		return view('admin/exam/readingtopic_edit', $data);
	}

	/**
	 * 编辑习题
	 */
	public function postUpdate(Request $request) {
		$id = intval($request->input('id'));
		$row = Examtopic::find($id);
		if (empty($row)) {
			return $this->error('无效的题目');
		}
		switch ($row->type) {
		case 1:
			$topic = strval($request->input('topic'));
			if (empty($topic)) {
				return $this->error('无效的题目');
			}
			$opts = $request->input('options');
			if (!is_array($opts)) {
				return $this->error('参数填写不完整');
			}
			$options = [];
			foreach ($opts as $k => $opt) {
				$options[chr($k + 65)] = $opt;
			}
			$options = json_encode($options);
			$answer = strval($request->input('answer'));
			$data = compact('topic', 'options', 'answer');
			foreach ($data as $key => $val) {
				$row->$key = $val;
			}
			$row->save();
			return $this->response($row);
		case 2:
			$topic = strval($request->input('topic'));
			if (empty($topic)) {
				return $this->error('无效的题目');
			}
			$row->topic = $topic;
			$answer = $request->input('answer');
			if (!is_array($answer)) {
				return $this->error('参数填写不完整');
			}
			$row->answer = json_encode($answer);
			$row->save();
			return $this->response($row);
		case 3:
			return $this->updateClozetestTopic($request);
		case 4:
			return $this->updateReadingTopic($request);
		default:
			return $this->error('无效的题型');
		}
	}

	/**
	 * 完形填空题编辑
	 */
	private function updateClozetestTopic($request) {
		$id = intval($request->input('id'));
		$trow = Examtopic::find($id);
		$trow->topic = safe2($request->input('topic'));
		$trow->save();

		$opts = $request->input('options');
		$answers = $request->input('answer');
		if (!is_array($opts) OR !is_array($answers)) {
			return $this->error('参数填写不完整');
		}

		Examtopic::where('f_id', $trow->id)->delete();
		foreach ($answers as $i => $answer) {
			$arr = array_slice($opts, $i * 4, 4);
			$options = [];
			foreach ($arr as $k => $v) {
				$options[chr($k + 65)] = $v;
			}
			$options = json_encode($options);
			$f_id = $trow->id;
			$topic = $i + 1;
			$data = compact('f_id', 'topic', 'options', 'answer');
			$row = new Examtopic;
			foreach ($data as $key => $val) {
				$row->$key = $val;
			}
			$row->save();
		}
		return $this->response($trow);
	}

	/**
	 * 编辑阅读理解题
	 */
	private function updateReadingTopic($request) {
		$id = intval($request->input('id'));
		$trow = Examtopic::find($id);
		$trow->topic = safe2($request->input('topic'));
		$trow->save();

		$subtopics = $request->input('subtopic');
		$opts = $request->input('options');
		$answers = $request->input('answer');
		if (!is_array($subtopics) OR !is_array($opts) OR !is_array($answers)) {
			return $this->error('参数填写不完整');
		}

		Examtopic::where('f_id', $trow->id)->delete();
		foreach ($subtopics as $i => $topic) {
			$arr = array_slice($opts, $i * 4, 4);
			$options = [];
			foreach ($arr as $k => $v) {
				$options[chr($k + 65)] = $v;
			}
			$options = json_encode($options);
			$f_id = $trow->id;
			$answer = $answers[$i];
			$data = compact('f_id', 'topic', 'options', 'answer');
			$row = new Examtopic;
			foreach ($data as $key => $val) {
				$row->$key = $val;
			}
			$row->save();
		}
		return $this->response($trow);
	}

	/**
	 * 删除习题
	 */
	public function postDelete(Request $request) {
		$id = intval($request->input('id'));
		$row = Examtopic::find($id);
		if (empty($row)) {
			return $this->error('无效的题目');
		}

		Examtopic::where('f_id', $id)->delete();
		if ($row->delete()) {
			return $this->response($id);
		}
		return $this->error('操作失败');
	}

}
